﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Net;

namespace CommonUtils
{
    public class ApiResult
    {
        /// <summary>
        /// 标准数据结构
        /// </summary>
        public static JObject GetStandardStruct(bool success = true, int code = 0, string message = "Success", object data = null)
        => new JObject {
            { "Success", success },
            {  "Code", code },
            { "Message", message },
            {  "Data", data.ToJToken() }
        };

        private static readonly IActionResult _ok = new OkObjectResult(GetStandardStruct());

        private static readonly IActionResult _error = new OkObjectResult(GetStandardStruct(false, -1, "服务器异常"));

        public static readonly IActionResult Unlogin = new OkObjectResult(GetStandardStruct(false, -2, "登录状态失效"));

        public static readonly IActionResult ArgsError = new OkObjectResult(GetStandardStruct(false, -3, "参数异常"));

        public static readonly IActionResult ParamsError = ArgsError;

        public static readonly IActionResult NoData = new OkObjectResult(GetStandardStruct(false, -4, "无数据"));

        public static IActionResult Ok()
        => _ok;

        public static IActionResult Ok(object data)
        => new OkObjectResult(GetStandardStruct(true, 0, "Success", data));

        public static IActionResult Ok(string msg)
        => new OkObjectResult(GetStandardStruct(true, 0, msg, null));

        public static IActionResult Error()
        => _error;

        public static IActionResult Error(string message)
        => new OkObjectResult(GetStandardStruct(false, -1, message));

        public static IActionResult Error(int code, string message)
        => new OkObjectResult(GetStandardStruct(false, code, message));
    }
}
